<template>
	<view class="read">
		<view class="read-book">
			<p v-html="bookText"></p>
		</view>
		<view class="bottom">
			<view class="list" @click="up()">
				上一章
			</view>
			<view class="list" @click="down()">
				下一章
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'read',
		data() {
			return {
				bookId: '',
				book: [],
				catalogId: '',
				bookText: '',
				chapterList: [],
				bookImg: '',
				author: '',
				chapter: '',
				userInfo: [],
				user: [],
			};
		},
		onBackPress() {
			this.back();
			return true;
		},
		methods: {
			back() {
				uni.navigateTo({
					url: '../catalog/catalog?id=' + this.bookId,
				})
			},
			getBookContent() {
				uni.request({
					url: `https://apis.netstart.cn/yunyuedu/reader/book/content.json?source_uuid=${this.bookId}&content_uuid=${this.catalogId}`,
					method: 'GET'
				}).then(res => {
					// console.log('res ==> ',res);
					this.bookText = res.data.data.content
					// console.log('this.bookText ==> ', this.bookText);
					uni.hideLoading()
				}).catch(err => {
					console.log('err ==> ', err);
				})
			},
			getChapter() {
				uni.request({
					url: `https://apis.netstart.cn/yunyuedu/reader/book/info.json?source_uuid=${this.bookId}`,
					method: 'GET'
				}).then(res => {
					// console.log('res ==> ', res);
					this.chapterList = res.data.data.catalog
					// console.log('this.chapterList ==> ', this.chapterList);
					// uni.hideLoading()
				}).catch(err => {
					console.log('err ==> ', err);
				})
			},
			goRead(id) {
				uni.navigateTo({
					url: `../read/read?bookId=${this.bookId}&catalogId=${id}`
				})
			},

			up() {
				this.chapterList.some((item, index) => {
					if (item.uuid == this.catalogId) {
						if (index == 0) {
							// console.log('ccc');
							uni.showToast({
								title: "已经是第一章了",
								icon: 'none'
							})
						} else {
							index--
							// console.log(this.chapterList[index].uuid);
							this.goRead(this.chapterList[index].uuid);
						}

					}
				})
			},

			down() {
				this.chapterList.some((item, index) => {
					if (item.uuid == this.catalogId) {
						if (index == this.chapterList.length) {
							uni.showToast({
								title: "已经是最后一章了",
								icon: 'none'
							})
						} else {
							index++
							// console.log(this.chapterList[index].uuid);
							this.goRead(this.chapterList[index].uuid);
						}

					}
				})
			},
			getBook() {
				uni.request({
					url: `https://apis.netstart.cn/yunyuedu/book/getsub.json?id=${this.bookId}`,
					method: 'GET'
				}).then(book => {
					// console.log('book ==> ', book);
					this.book = book.data.feed.entry
					this.bookImg = book.data.feed.entry.link[1].href
					this.author = book.data.feed.entry.author.name
					// console.log('this.book ==> ', this.book);
					this.addHistory();
				}).catch(err => {
					console.log('book ==> ', err);
				})
			},
			addHistory() {
				let index = this.chapterList.findIndex((item) => item.secId === this.catalogId);
				this.chapter = this.chapterList[index].title;
				let history = {
					bookImg: this.bookImg,
					author: this.author,
					title: this.book.title,
					bookId: this.bookId,
					catalogId: this.catalogId,
					chapter: this.chapter,
				}
				// console.log('index ==> ',index);
				// console.log('history ==> ', history);
				this.getUser(history);
			},
			getUser(history) {
				let user = uni.getStorageSync('userInfo');
				if (!user) {
					return
				}
				this.userInfo = JSON.parse(user);
				uniCloud.callFunction({
					name: 'getUser',
					data: {
						phone: this.userInfo.phone
					}
				}).then(res => {
					// console.log('res ==> ', res);
					this.user = res.result.data[0];
					// console.log('this.user ==> ',this.user);
					console.log(this.user.history);
					if (this.user.history.length == 0) {
						this.user.history.push(history)
					}
					let index = this.user.history.findIndex((item) => item.bookId === history.bookId);
					// console.log(index);
					if (index < 0) {
						this.user.history.push(history)
					} else {
						this.user.history[index] = history
					}
					uniCloud.callFunction({
						name: 'addHistory',
						data: {
							phone: this.userInfo.phone,
							history: this.user.history
						}
					}).then(res => {
						console.log('res ==> ', res);
					}).catch(err => {
						console.log('err ==> ', err);
					})
				}).catch(err => {
					console.log('err ==> ', err);
				})
			}
		},

		onLoad(option) {
			uni.showLoading({
				title: '正在加载中...'
			})
			this.bookId = option.bookId;
			this.catalogId = option.catalogId;
			this.getBookContent();
			this.getChapter();
			this.getBook();
		}
	}
</script>

<style lang="scss">
	.read {
		width: 100%;
		position: relative;
		background-color: #fceccc;
		height: 600vh;

		.read-book {
			background-color: #fceccc;
			padding: 20rpx 40rpx;
			margin-bottom: 100rpx;
			font-size: 28rpx;

			p {
				line-height: 2.8 !important;
				margin-bottom: 100rpx;
			}
		}

		.bottom {
			width: 100%;
			height: 100rpx;
			background-color: #F0CEAD;
			position: fixed;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: space-evenly;

			.list {
				width: 200rpx;
				height: 80rpx;
				text-align: center;
				line-height: 80rpx;
				background-color: #FCECCC;
				border-radius: 80rpx;
			}
		}
	}
</style>